/*
 * ©2023 JOY-HUBS.COM
 */

package com.joyhubs.base.entity;

import java.io.Serializable;

import lombok.Data;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.joyhubs.base.util.JsonUtil;

/**
 * 请求反馈协议对象
 *
 * @author zhiwu.zzw
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ContentBody implements Serializable {
    
    public static final String CODE_SUCCESS = "0";
    public static final String CODE_FAILED = "1";
    
    private String code;
    private boolean success;
    private String message;
    private Object content;
    
    /**
     * 成功
     *
     * @return
     */
    public static ContentBody success() {
        return success(null);
    }
    
    public static ContentBody success(Object data) {
        ContentBody body = new ContentBody();
        body.setCode(CODE_SUCCESS);
        body.setSuccess(true);
        body.setContent(data);
        return body;
    }
    
    public static ContentBody error(String code, String msg) {
        ContentBody body = new ContentBody();
        body.setCode(code);
        body.setMessage(msg);
        body.setSuccess(false);
        return body;
    }
    
    public static ContentBody error(Object data) {
        ContentBody body = new ContentBody();
        body.setCode(CODE_FAILED);
        body.setSuccess(false);
        body.setContent(data);
        return body;
    }
    
    @Override
    public String toString() {
        return JsonUtil.toJson(this).orElse("");
    }
    
}
